<?php 
$this->beginWidget('zii.widgets.jui.CJuiDialog',array(
                        'id'=>'fileDialog',
                        'options'=>array(
                        'title'=>'File',
                        'autoOpen'=>true,
                        'modal'=>true,
                        'width'=>970,
                        'height'=>520,
                        'resizable' => false,
                        ),
                ));
echo $this->renderPartial('/media/_browser');
echo '<table class="form" style="width: 100%">';
echo'<tr>
            <td colspan="2" class="cbutton">';
echo CHtml::htmlButton('Insert',array('id'=>'btnInsert'));     
echo CHtml::htmlButton('Cancel',array('id'=>'btnInsert','onclick'=>'$("#fileDialog").dialog("close");'));
echo'</td>
        </tr>';
echo'</table> ';
?>
<?php $this->endWidget('zii.widgets.jui.CJuiDialog');?>
<script type="text/javascript">
$(document).ready(function () {
    function getElementsByClassName(node,classname) {
        if (node.getElementsByClassName) { // use native implementation if available
            return node.getElementsByClassName(classname);
        } else {
            return (function getElementsByClass(searchClass,node) {
                if ( node == null )
                node = document;
                var classElements = [],
                    els = node.getElementsByTagName("*"),
                    elsLen = els.length,
                    pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;

                for (i = 0, j = 0; i < elsLen; i++) {
                    if ( pattern.test(els[i].className) ) {
                        classElements[j] = els[i];
                        j++;
                    }
                }
                return classElements;
            })(classname, node);
        }
    }
$('#btnInsert').click(function(){
        fileName = getElementsByClassName(document, "elfinder-cwd-filename ui-draggable ui-state-hover");        
        filePathNodes = getElementsByClassName(document, "elfinder-path");
        filePath = filePathNodes[0].getElementsByTagName("a");
        if(fileName.length == 0){
            alert("Please choose item before insert.");                        
        }
        else if(fileName.length > 1){
            alert("Please choose only 1 item.");                        
        }
        else{                        
            path = "";
            for(i = 0; i < filePath.length ; i++) {                                                                
                path += filePath[i].innerHTML + "/";
            }                                        
            $("#txtPath").val(path + fileName[0].innerHTML);
            $('#fileDialog').dialog('close');            
        }
});
   
        
});
</script>